package com.rankeiot.platform.controller;

import cn.hutool.http.HttpUtil;
import com.rankeiot.platform.config.AMapConfig;
import jakarta.servlet.http.HttpServletRequest;
import org.springframework.http.MediaType;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class MapController {
    private static final String baseAddr="https://restapi.amap.com/v3/";
    @GetMapping("/map/regeo")
    public ResponseEntity regeo(String location){
       String body= HttpUtil.get(baseAddr+"geocode/regeo?key="+ AMapConfig.APIKey.strValue() +"&radius=50&extensions=Address&location="+location);
        return ResponseEntity.ok().contentType(MediaType.APPLICATION_JSON).body(body);
       // return restTemplate.getForEntity("https://restapi.amap.com/v3/geocode/regeo?key=7d1a6efe535a021e0957e835953a795f&radius=50&extensions=Address&location="+location, Map.class);
    }
    @GetMapping("/_AMapService/**")
    public ResponseEntity proxy(HttpServletRequest request){
        //_AMapService
        String uri= request.getRequestURI();
        uri =  uri.replaceFirst("^/_AMapService","");

        byte[] body= HttpUtil.createGet("https://restapi.amap.com"+uri+"?"+request.getQueryString()+"&jscode="+AMapConfig.JSCode.strValue()).execute().bodyBytes();
        return ResponseEntity.ok().contentLength(body.length).contentType(MediaType.parseMediaType("application/javascript")).body(body);
    }
//    public ResponseEntity img(String location){
//        String url=baseAddr+"staticmap?location="+location+"&markers=large,,A:"+location+"&zoom=10&size=400*400&key="+AMapConfig.APIKey.strValue();
//        HttpUtil.createGet("").execute().bodyBytes();
//    }
}
